import type { App } from "vue";

// 监听元素变化 或者窗口变化
export default function (_vue: App) {

  _vue.directive('resize', {
    beforeMount(el, binding) {
      let width = '', height = '';
      function isResize() {
        const style = document.defaultView?.getComputedStyle(el);
        if (width !== style?.width || height !== style?.height) {
          binding.value();  // 执行传入的方法
        }
        width = style?.width || '';
        height = style?.height || '';
      }
      el.__timer__ = setInterval(isResize, 300); // 周期性监听元素是否改变
    },
    unmounted(el) {
      clearInterval(el.__timer__);
    }
  })

}
